inherit CometDimitri Logical_consistency

#___________________________________________________________________________________________________________________________________________
method CometDimitri constructor {name descr args} {
 this inherited $name $descr
 this set_GDD_id CT_CometDimitri

 set CFC ${objName}_CFC; CometDimitri_CFC $CFC; this set_Common_FC $CFC
 $CFC set_LC $objName

 set this(LM_FC) ${objName}_LM_FC
 CometDimitri_LM_FC $this(LM_FC) $this(LM_FC) "The LM FC of $name"
   this Add_LM $this(LM_FC)
 set this(LM_LP) ${objName}_LM_LP
 CometDimitri_LM_LP $this(LM_LP) $this(LM_LP) "The LM LP of $name"
   this Add_LM $this(LM_LP)
   
# set up inner graph
 set this(cont_compo)   [CPool get_a_comet CometContainer -Add_style_class [list CONT COMPONENTS]]
 set this(choice_class) [CPool get_a_comet CometChoice    -Add_style_class [list TOOLS CHOICE]]
   $this(choice_class) Subscribe_to_set_currents $objName "$objName set_class_selected \[$this(choice_class) get_currents\]" UNIQUE

# Finish the build
 eval "$objName configure $args"
 return $objName
}

#___________________________________________________________________________________________________________________________________________
method CometDimitri dispose {} {this inherited}

#___________________________________________________________________________________________________________________________________________
#___________________________________________________________________________________________________________________________________________
#___________________________________________________________________________________________________________________________________________
Methodes_set_LC CometDimitri [P_L_methodes_set_CometDimitri] {$this(FC)} {$this(L_LM)}
Methodes_get_LC CometDimitri [P_L_methodes_get_CometDimitri] {$this(FC)}

#___________________________________________________________________________________________________________________________________________
Generate_accessors CometDimitri [list cont_compo choice_class]

#___________________________________________________________________________________________________________________________________________
Manage_CallbackList CometDimitri [list set_L_instance_compo Add_L_instance_compo Reset PlayPause Step] end

#___________________________________________________________________________________________________________________________________________
Inject_code CometDimitri set_L_instance_compo {
 foreach e $v {$e set_C_Magellan $objName}
} {} __UDPATE_reference_to_comet_Magellan__

#___________________________________________________________________________________________________________________________________________
Inject_code CometDimitri Add_L_instance_compo {
 foreach e $v {$e set_C_Magellan $objName}
} {} __UDPATE_reference_to_comet_Magellan__

#___________________________________________________________________________________________________________________________________________
Inject_code CometDimitri set_L_instance_compo {
  $this(cont_compo) set_daughters_R $v
  foreach C $v {$C Subscribe_to_dispose $objName "$objName Sub_L_instance_compo $C"}
} {} __Add_component_to_the_canvas__

#___________________________________________________________________________________________________________________________________________
Inject_code CometDimitri Add_L_instance_compo {
  $this(cont_compo) Add_daughters_R $v
  foreach C $v {$C Subscribe_to_dispose $objName "$objName Sub_L_instance_compo $C"}
} {} __Add_component_to_the_canvas__

#___________________________________________________________________________________________________________________________________________
Inject_code CometDimitri Sub_L_instance_compo {
  $this(cont_compo) Sub_daughters_R $v
  foreach C $v {
	  set L_connexions_to_delete [list]
	  foreach connexion [this get_L_connexions] {
		lassign $connexion out in
		  lassign $out C_out port_out
		  lassign $in  C_in  port_in
		if {$C == [$C_out get_LC] || $C == [$C_in get_LC]} {lappend L_connexions_to_delete $connexion}
      }
   }
  #puts "  $objName Sub_L_connexions [list $L_connexions_to_delete]" 
  this Sub_L_connexions $L_connexions_to_delete
} {}

#___________________________________________________________________________________________________________________________________________
Inject_code CometDimitri set_class_selected {
 if {[this get_class_selected] == [$this(choice_class) get_currents]} {return}
} {}

#___________________________________________________________________________________________________________________________________________
Inject_code CometDimitri set_L_class_compo {
  $this(choice_class) set_daughters_R $v
} {}

#___________________________________________________________________________________________________________________________________________
Inject_code CometDimitri Add_L_class_compo {
  $this(choice_class) Add_daughters_R $v
} {}

#___________________________________________________________________________________________________________________________________________
Inject_code CometDimitri Sub_L_class_compo {
  $this(choice_class) Sub_daughters_R $v
} {}


#___________________________________________________________________________________________________________________________________________
#___________________________________________________________________________________________________________________________________________
#___________________________________________________________________________________________________________________________________________
method CometDimitri Save_network_in_file {f_name} {
 set f [open $f_name w]
   puts $f "<MIGA id=\"$objName\">"
   foreach compo [this get_L_instance_compo] {
     puts $f [$compo Serialize $objName]
    }
   foreach connexion [this get_L_connexions] {
     lassign $connexion out in
     lassign $out C_out port_out
     lassign $in  C_in  port_in
	 puts $f "<connexion src=\"${objName}_$C_out $port_out\" dest=\"${objName}_$C_in $port_in\"/>"
    }
   puts $f "</MIGA>"
 close $f
}

#___________________________________________________________________________________________________________________________________________
method CometDimitri Load_network_from_file {f_name} {
 foreach c [this get_L_instance_compo] {$c dispose}
 
 set f [open $f_name r]
   dom parse [read $f] doc
   $doc documentElement root
   set L_rep [$root asList]
 close $f

 # Parse the list
 set this(L_mappings_for_load) [list]
 foreach {var val} [lindex $L_rep 1] {set $var $val}
 set prefix $id
 foreach e [lindex $L_rep 2] {
   set this(L_mappings_for_load) [concat $this(L_mappings_for_load) [this Load_[lindex $e 0] $e]]
  }
}

#___________________________________________________________________________________________________________________________________________
method CometDimitri Load_CometCompo_evolution {str} {
 set MetaData ""; set nb_max_process *
 foreach {var val} [lindex $str 1] {set $var $val}
# Create a CometCompo_evolution
 set CometCompo [CPool get_a_comet CometCompo_evolution]
 $CometCompo set_name       $name; if {$core_f_name != ""} {$CometCompo load_core [Root_of_CometDimitri]$core_f_name}
 $CometCompo set_L_MetaData     $MetaData
 foreach {s v} $MetaData {$CometCompo Add_style_class $s}
 $CometCompo set_nb_max_process $nb_max_process
 
 #puts "  $CometCompo set_L_MetaData {$MetaData}"
 
 set miga_name $id
 
 set L_post_cmd [list]
 
 foreach e [lindex $str 2] {
   foreach {var value} [lindex $e 1] {set $var $value}
   switch [lindex $e 0] {
     input   {$CometCompo Add_L_inputs  [list [list $id $cardN]]
	         }
     output  {$CometCompo Add_L_outputs [list [list $id $cardN]]
	         }
     param   {$CometCompo Add_L_param   [list [list $id $val]]
	         }
     default {lappend L_post_cmd $e
	         }
    }
  }

 this Add_L_instance_compo $CometCompo
 
 foreach cmd $L_post_cmd {
   lassign $cmd PM_type L_ops 
   foreach PM [CSS++ $objName "#$objName->PMs(${PM_type}.$CometCompo)"] {
	 foreach {var value} $L_ops {eval "$PM $var $value"}
    }
  }
 
 return [list $miga_name $CometCompo]
}

#___________________________________________________________________________________________________________________________________________
method CometDimitri Load_connexion {str} {
 set str [string map $this(L_mappings_for_load) $str]
 
 foreach {var value} [lindex $str 1] {set $var $value}
 
 this Add_L_connexions [list [list $src $dest]]
 
 return ""
}


